package com.jy.adscm.zltx.controller;


import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import com.jy.adscm.zltx.bean.SjApplyUpdateRecord;
import com.jy.adscm.zltx.service.SjApplyUpdateRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2018-11-08
 */
@RestController
@RequestMapping("/adscm/zltx/sjApplyUpdateRecord")
public class SjApplyUpdateRecordController {
     @Autowired
     SjApplyUpdateRecordService SjApplyUpdateRecordService;
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
     @RequestMapping("insert")
     public SimpleCoreResponse insert(@RequestBody SjApplyUpdateRecord  sjApplyUpdateRecord, HttpServletRequest request)throws AdscmSystemException {
         String tokenId = request.getParameter("tokenId");
         Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
         if (token==null){
             throw new AdscmSystemException("未找到登录信息");
         }
         sjApplyUpdateRecord.setApplicant(token.getUser().getId().toString());
         sjApplyUpdateRecord.setApplyTime(new Date());
         sjApplyUpdateRecord.setApprovalResult("-1");

         boolean b = SjApplyUpdateRecordService.insertSjApplyUpdateRecord(sjApplyUpdateRecord);
         return  new SimpleCoreResponse("200","新增成功",b);
     }
    @RequestMapping("update")
     public  SimpleCoreResponse update(@RequestBody SjApplyUpdateRecord sjApplyUpdateRecord,HttpServletRequest request)throws  AdscmSystemException{
         boolean b = SjApplyUpdateRecordService.updateById(sjApplyUpdateRecord);
         return  new SimpleCoreResponse("200","修改成功",b);
     }
     @RequestMapping("selectByFileId/{fileId}")
     public SimpleCoreResponse selectByFileId(@PathVariable Integer fileId )throws AdscmSystemException{
         List<SjApplyUpdateRecord> sjApplyUpdateRecords = SjApplyUpdateRecordService.selectByFileId(fileId);
         return  new SimpleCoreResponse("200","查询成功",sjApplyUpdateRecords);
     }

}

